package com.yaozheng.vocationaltraining.view;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.paper.ItemQuestionTitleView;
import com.yaozheng.vocationaltraining.view.question.BaseQuestionView;
import com.yaozheng.vocationaltraining.view.question.MultipleChoiceQuestionView_;
import com.yaozheng.vocationaltraining.view.question.SingleChoiceQuestionView_;
import com.yaozheng.vocationaltraining.view.question.TrueOrFalseQuestionView_;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

@EViewGroup(R.layout.item_fragment_question)
public class ItemQuestionView extends LinearLayout implements
        BaseQuestionView.OnAnswerChange {
    BaseQuestionView baseQuestionView;
    @ViewById
    FrameLayout questionContentOptionsFrame;
    @ViewById
    ItemQuestionTitleView questionContentText;

    JSONObject itemData;

    public ItemQuestionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated construc tor stub
    }

    public ItemQuestionView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void loadData(int position, JSONObject itemData) {
        this.itemData = itemData;
        JSONArray options = TypeUtils.getJsonArray(itemData, "options");
        boolean multiple = TypeUtils.getJsonBoolean(itemData, "multiple");
        if (multiple) {
            baseQuestionView = MultipleChoiceQuestionView_.build(getContext());
        } else {
            if (TypeUtils.getJsonArraySize(options) == 2) {
                baseQuestionView = TrueOrFalseQuestionView_.build(getContext());
            } else {
                baseQuestionView = SingleChoiceQuestionView_
                        .build(getContext());
            }
        }
        baseQuestionView.setAnswer(TypeUtils.getJsonString(itemData, "answer"));
        questionContentText.setContentValue(TypeUtils
                .getJsonString(itemData, "subject"));
        baseQuestionView.setOnAnswerChange(this);
        if (TypeUtils.getJsonArraySize(options) == 2) {
            baseQuestionView.setOptionsContent("A：" + TypeUtils.getJsonString(options, 0), "B：" + TypeUtils.getJsonString(options, 1));
        } else {
            baseQuestionView.setOptionsContent("A：" + TypeUtils.getJsonString(options, 0), "B：" + TypeUtils.getJsonString(options, 1)
                    , "C：" + TypeUtils.getJsonString(options, 2), "D：" + TypeUtils.getJsonString(options, 3)
            );
        }
        JSONArray optionsCard = TypeUtils.getJsonArray(itemData, "optionsCard");
        baseQuestionView.optionsCard(optionsCard);
        questionContentOptionsFrame.addView(baseQuestionView);
    }

    @Override
    public void answerChange(BaseQuestionView baseQuestionView) {
        String answer = baseQuestionView.getAnswer();
        if (answer == null) {
            itemData.remove("answer");
            itemData.put("questionStatus", 0);
        } else {
            itemData.put("answer", answer);
            itemData.put("questionStatus", 1);
        }
    }

}
